<?php
    Class Solution{
        /**
         * 326. 3 的幂
         * @param Integer $n
         * @return Boolean
         */
        function isPowerOfThree326($n) {
            while($n > 1){
                if($n % 3 != 0){
                    return false;
                }
                $n /= 3;
            }
            if($n <1){
                return false;
            }

            return true;
        }

        /**
         * 338. 比特位计数
         * @param Integer $n
         * @return Integer[]
         */
        function countBits338($n) {
            if($n == 0){
                return [0];
            }
            if($n == 1){
                return [0,1];
            }
            $arr = [0,1];
            $mi  = 2;
            for($i = 2; $i < $n+1; $i++){
                if($i % $mi == 0){
                    $arr[] = 1;
                    $mi = $i;
                }else{
                    $arr[] = $arr[$i-$mi]+1;
                }
            }

            return $arr;
        }

        /**
         * 342. 4的幂
         * @param Integer $n
         * @return Boolean
         */
        function isPowerOfFour342($n) {
            return log($n,4) == intval(log($n,4));
        }

        /**
         * 344. 反转字符串
         * @param String[] $s
         * @return NULL
         */
        function reverseString344(&$s) {
            $len = count($s);
            for($i = 0; $i < $len/2; $i++) {
                $temp = $s[$i];
                $s[$i] = $s[$len-1-$i];
                $s[$len-1-$i] = $temp;
            }

            return $s;
        }

        /**
         * 345. 反转字符串中的元音字母
         * @param String $s
         * @return String
         */
        function reverseVowels345($s) {
            $left  = 0;
            $right = strlen($s);
            while($left < $right) {
                if(in_array($s[$left],['a','e','i','o','u','A','E','I','O','U'])){
                    $temp = $s[$left];
                }else{
                    $left++;
                }
                if(in_array($s[$right],['a','e','i','o','u','A','E','I','O','U'])){
                    $temp2 = $s[$right];
                }else{
                    $right--;
                }
                if($temp && $temp2){
                    $s[$left] = $temp2;
                    $s[$right] = $temp;
                    $temp = $temp2 = 0;
                    $left++;
                    $right--;
                }
            }

            return $s;
        }

        /**
         * 349. 两个数组的交集
         * @param Integer[] $nums1
         * @param Integer[] $nums2
         * @return Integer[]
         */
        function intersection349($nums1, $nums2) {
            $arr  = [];
            $hash = [];
            foreach($nums1 as $num) {
                if(!isset($hash[$num])){
                    $hash[$num] = $num;
                }
            }
            foreach($nums2 as $num) {
                if(isset($hash[$num]) && !$arr[$num]){
                    $arr[$num] = $hash[$num];
                }
            }

            return $arr;
        }

        /**
         * 350. 两个数组的交集 II
         * @param Integer[] $nums1
         * @param Integer[] $nums2
         * @return Integer[]
         */
        function intersect350($nums1, $nums2) {
            $hash = [];
            $arr  = [];
            foreach($nums1 as $num) {
                if(!isset($hash[$num])){
                    $hash[$num] = 0;
                }
                $hash[$num]++;
            }
            foreach($nums2 as $num) {
                if(isset($hash[$num])){
                    $arr[] = $num;
                    $hash[$num]--;
                    if(!$hash[$num]){
                        unset($hash[$num]);
                    }
                }
            }

            return $arr;
        }

        /**
         * 367. 有效的完全平方数
         * @param Integer $num
         * @return Boolean
         */
        function isPerfectSquare367($num) {
            $start = 1;
            $end   = $num;
            $mid = intval(($start + $end) / 2);
            while($mid*$mid != $num) {
                if((($end - $start) == 0) || (($end - $start) == 1)){
                    return false;
                }
                if($mid*$mid < $num) {
                    $start = $mid;
                }else {
                    $end = $mid;
                }
                $mid = intval(($start + $end) / 2);
            }

            return true;
        }

        /**
         * 374. 猜数字大小
         * @param  Integer  $n
         * @return Integer
         */
        function guessNumber($n) {
            $left  = 0;
            $right = $n;
            $half  = $n / 2;
            $guess = $this->guess($half);
            while($guess != 0) {
                if($guess > 0) {
                    $left  = $half;
                }else {
                    $right = $half;
                }
                $half = ($left + $right)/2;
                $guess = $this->guess($half);
            }
            return $half;
        }
        function guess($n)
        {
            $x = 10;
            if($n < $x){
                return 1;
            }else if ($n > $x) {
                return -1;
            }else{
                return 0;
            }
        }

        /**
         * 383. 赎金信
         * @param String $ransomNote
         * @param String $magazine
         * @return Boolean
         */
        function canConstruct383($ransomNote, $magazine) {
            $lenA = strlen($ransomNote);
            $lenB = strlen($magazine);
            $map = [];
            for($i = 0; $i < $lenB; $i++) {
                if(!isset($map[$magazine[$i]])) {
                    $map[$magazine[$i]] = 1;
                }else{
                    $map[$magazine[$i]]++;
                }
            }
            print_r($map);
            for($i = 0; $i < $lenA; $i++) {
                if(!$map[$ransomNote[$i]]) {
                    return false;
                }
                $map[$ransomNote[$i]]--;
            }

            return true;
        }

    }

    $solution = new Solution();
$ransomNote = 'aa';
$magazine = 'ab';
    print_r($solution->canConstruct383($ransomNote, $magazine));



?>